﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_SaaS_WinForm.生产计划_派单_车间
{
    public partial class NavigationForm : Form
    {
        public NavigationForm()
        {
            InitializeComponent();
            InitializeNavigationForm();
        }

        /// <summary>
        /// 初始化导航窗体
        /// </summary>
        private void InitializeNavigationForm()
        {
            try
            {
                // 设置窗体属性
                this.Text = "生产计划+派单+车间 - 导航页";
                this.Size = new Size(1000, 700);
                this.StartPosition = FormStartPosition.CenterScreen;
                this.BackColor = Color.FromArgb(240, 240, 240);
                this.FormBorderStyle = FormBorderStyle.FixedDialog;
                this.MaximizeBox = false;
                this.MinimizeBox = true;

                // 创建顶部标题面板
                CreateHeaderPanel();

                // 创建导航卡片面板
                CreateNavigationCards();

                // 设置窗体图标
                SetFormIcon();

                Console.WriteLine("生产计划+派单+车间导航窗体初始化完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"初始化导航窗体时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 创建顶部标题面板
        /// </summary>
        private void CreateHeaderPanel()
        {
            Panel headerPanel = new Panel
            {
                Dock = DockStyle.Top,
                Height = 80,
                BackColor = Color.FromArgb(52, 152, 219)
            };

            // 主标题
            Label lblMainTitle = new Label
            {
                Text = "生产计划+派单+车间管理系统",
                Font = new Font("微软雅黑", 16, FontStyle.Bold),
                ForeColor = Color.White,
                TextAlign = ContentAlignment.MiddleCenter,
                Dock = DockStyle.Top,
                Height = 40
            };

            // 副标题
            Label lblSubTitle = new Label
            {
                Text = "请选择要进入的功能模块",
                Font = new Font("微软雅黑", 10),
                ForeColor = Color.FromArgb(230, 230, 230),
                TextAlign = ContentAlignment.MiddleCenter,
                Dock = DockStyle.Top,
                Height = 25
            };

            // 返回按钮
            Button btnBack = new Button
            {
                Text = "返回主菜单",
                Size = new Size(90, 30),
                Location = new Point(20, 25),
                BackColor = Color.FromArgb(231, 76, 60),
                ForeColor = Color.White,
                FlatStyle = FlatStyle.Flat,
                Font = new Font("微软雅黑", 8)
            };
            btnBack.Click += BtnBack_Click;

            headerPanel.Controls.Add(lblMainTitle);
            headerPanel.Controls.Add(lblSubTitle);
            headerPanel.Controls.Add(btnBack);

            this.Controls.Add(headerPanel);
        }

        /// <summary>
        /// 创建导航卡片
        /// </summary>
        private void CreateCard(TableLayoutPanel parent, int row, int col, string title, string icon, string description, Action onClick)
        {
            Panel card = new Panel
            {
                Dock = DockStyle.Fill,
                BackColor = Color.White,
                Margin = new Padding(10),
                Padding = new Padding(15)
            };

            // 图标
            Label lblIcon = new Label
            {
                Text = icon,
                Font = new Font("Segoe UI Emoji", 28, FontStyle.Regular),
                ForeColor = Color.FromArgb(52, 152, 219),
                TextAlign = ContentAlignment.MiddleCenter,
                Dock = DockStyle.Top,
                Height = 70
            };

            // 标题
            Label lblTitle = new Label
            {
                Text = title,
                Font = new Font("微软雅黑", 12, FontStyle.Bold),
                ForeColor = Color.FromArgb(44, 62, 80),
                TextAlign = ContentAlignment.MiddleCenter,
                Dock = DockStyle.Top,
                Height = 30
            };

            // 描述
            Label lblDescription = new Label
            {
                Text = description,
                Font = new Font("微软雅黑", 8),
                ForeColor = Color.FromArgb(127, 140, 141),
                TextAlign = ContentAlignment.MiddleCenter,
                Dock = DockStyle.Fill
            };

            card.Controls.Add(lblDescription);
            card.Controls.Add(lblTitle);
            card.Controls.Add(lblIcon);

            // 添加点击事件
            card.Click += (sender, e) => onClick();
            lblIcon.Click += (sender, e) => onClick();
            lblTitle.Click += (sender, e) => onClick();
            lblDescription.Click += (sender, e) => onClick();

            // 添加悬停效果
            card.MouseEnter += (sender, e) =>
            {
                card.BackColor = Color.FromArgb(245, 245, 245);
                card.Cursor = Cursors.Hand;
            };

            card.MouseLeave += (sender, e) =>
            {
                card.BackColor = Color.White;
                card.Cursor = Cursors.Default;
            };

            parent.Controls.Add(card, col, row);
        }

        /// <summary>
        /// 创建导航卡片面板
        /// </summary>
        private void CreateNavigationCards()
        {
            Panel cardsPanel = new Panel
            {
                Dock = DockStyle.Fill,
                BackColor = Color.FromArgb(236, 240, 241),
                Padding = new Padding(50)
            };

            // 创建2x3网格布局
            TableLayoutPanel gridPanel = new TableLayoutPanel
            {
                Dock = DockStyle.Fill,
                ColumnCount = 2,
                RowCount = 3,
                ColumnStyles = 
                {
                    new ColumnStyle(SizeType.Percent, 50F),
                    new ColumnStyle(SizeType.Percent, 50F)
                },
                RowStyles = 
                {
                    new RowStyle(SizeType.Percent, 33.33F),
                    new RowStyle(SizeType.Percent, 33.33F),
                    new RowStyle(SizeType.Percent, 33.33F)
                },
                Padding = new Padding(30)
            };

            // 创建五个导航卡片
            CreateCard(gridPanel, 0, 0, "生产计划管理", "📋", "管理生产计划和排程", () => OpenForm(new NewPlanMain()));
            CreateCard(gridPanel, 0, 1, "车间管理", "🏭", "管理车间、站点和生产线", () => OpenForm(new CarManagement()));
            CreateCard(gridPanel, 1, 0, "工单管理", "📝", "管理工单任务和派工", () => OpenForm(new WorkOrder()));
            CreateCard(gridPanel, 1, 1, "班组管理", "👥", "管理班组和人员", () => OpenForm(new TeamManage()));
           

            cardsPanel.Controls.Add(gridPanel);
            this.Controls.Add(cardsPanel);
        }

        /// <summary>
        /// 设置窗体图标
        /// </summary>
        private void SetFormIcon()
        {
            try
            {
                string[] possiblePaths = {
                    System.IO.Path.Combine(Application.StartupPath, "..", "..", "Images", "ww.ico"),
                    System.IO.Path.Combine(Application.StartupPath, "Images", "ww.ico")
                };

                string iconPath = possiblePaths.FirstOrDefault(System.IO.File.Exists);
                if (!string.IsNullOrEmpty(iconPath))
                {
                    this.Icon = new System.Drawing.Icon(iconPath);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"设置窗体图标时出错: {ex.Message}");
            }
        }

        /// <summary>
        /// 返回主菜单按钮点击事件
        /// </summary>
        private void BtnBack_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"返回主菜单时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 生产计划管理按钮点击事件
        /// </summary>
        private void BtnProductPlan_Click(object sender, EventArgs e)
        {
            try
            {
                var productPlanForm = new NewPlanMain();
                productPlanForm.Show();
                Console.WriteLine("已打开生产计划管理窗体");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打开生产计划管理失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 车间管理按钮点击事件
        /// </summary>
        private void BtnCarManagement_Click(object sender, EventArgs e)
        {
            try
            {
                var carManagementForm = new CarManagement();
                carManagementForm.Show();
                Console.WriteLine("已打开车间管理窗体");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打开车间管理失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }



        /// <summary>
        /// 打开窗体
        /// </summary>
        private void OpenForm(Form form)
        {
            try
            {
                form.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打开窗体失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        [System.Runtime.InteropServices.DllImport("Gdi32.dll")]
        private static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
    }
}
